

#include "usb_firmware_library_headers.h"
#include "usb_framework.h"
#include "usb_interrupt.h"
#include "usb_suspend.h"
#include "UsbFunctions.h"


void Usb_Irq_Init(void){
  usbfwInit();
  usbirqInit(USBIRQ_EVENT_SUSPEND | USBIRQ_EVENT_RESUME | USBIRQ_EVENT_RESET | USBIRQ_EVENT_SETUP | USBIRQ_EVENT_EP1IN);
  EA =1;
  HAL_USB_PULLUP_ENABLE();
}


void Usb_ProcessLoop(void) {
  
  
  
  // Handle reset signaling on the bus
  if (USBIRQ_GET_EVENT_MASK() & USBIRQ_EVENT_RESET) {
    USBIRQ_CLEAR_EVENTS(USBIRQ_EVENT_RESET);
    usbfwResetHandler();
    LED_RED_OFF();
  }

  // Handle packets on EP0
  if (USBIRQ_GET_EVENT_MASK() & USBIRQ_EVENT_SETUP)  {
    USBIRQ_CLEAR_EVENTS(USBIRQ_EVENT_SETUP);
    usbfwSetupHandler();
    LED_RED_ON();
  }
  
  if (USBIRQ_GET_EVENT_MASK() & USBIRQ_EVENT_EP1IN)  {
    USBIRQ_CLEAR_EVENTS(USBIRQ_EVENT_EP1IN);
  }

  // Handle USB suspend
  if (USBIRQ_GET_EVENT_MASK() & USBIRQ_EVENT_SUSPEND)  {
    // Clear USB suspend interrupt
    USBIRQ_CLEAR_EVENTS(USBIRQ_EVENT_SUSPEND);
    LED_RED_OFF();

    // Take the chip into PM1 until a USB resume is deteceted.
    usbsuspEnter();

    // Running again; first clear RESUME interrupt
    USBIRQ_CLEAR_EVENTS(USBIRQ_EVENT_RESUME);
  }
	
}



void usbsrHookClearFeature(void) {
  usbfwData.ep0Status = EP_STALL;
}
void usbsrHookSetFeature(void) {
  usbfwData.ep0Status = EP_STALL;
}

void usbsrHookProcessEvent(uint8 event, uint8 index)
{}
